package cst.neu.yuejian_31.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.activity.MasterDetailActivity;
import cst.neu.yuejian_31.bean.MasterBean;

/**
 * Created by Alienware on 2017/10/19.
 */

public class MasterAdapter extends RecyclerView.Adapter{
    private Context context;
    private LayoutInflater layoutInflater;
    private List<MasterBean> list;

    public MasterAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<MasterBean>list){
        this.list=list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView mastername,intro;
        public ViewHolder(View itemView){
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.master_pic);
            mastername= (TextView) itemView.findViewById(R.id.master_name);
            intro= (TextView) itemView.findViewById(R.id.intro);

        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item_master,parent,false);
        return new MasterAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final MasterBean master =list.get(position);
        if(master==null)
            return;
        MasterAdapter.ViewHolder viewHolder= (MasterAdapter.ViewHolder) holder;
        Picasso.with(context).load(Constant.MASTER_URL+master.getMaster_pic())
                .placeholder(R.drawable.julebu)
                .into(viewHolder.imageView);
        viewHolder.mastername.setText(master.getMaster_name());
        viewHolder.intro.setText(master.getIntro());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, MasterDetailActivity.class);
                intent.putExtra("master_id",master.getMaster_id());
                intent.putExtra("master_name",master.getMaster_name());
                intent.putExtra("master_pic",master.getMaster_pic());
                intent.putExtra("master_intro",master.getIntro());
                intent.putExtra("master_tel",master.getPhone());
                intent.putExtra("master_jointime",master.getJointime());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list==null)return 0;
        else
            return list.size();
    }
}